package com.ling.pl.autoproject.definition.controller;

import com.ling.pl.autoproject.definition.model.ProgrammeEntity;
import com.ling.pl.autoproject.definition.model.ProgrammeVO;
import com.ling.pl.autoproject.definition.model.ProjectVO;
import com.ling.pl.autoproject.definition.service.ProgrammeService;
import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.core.commons.utils.PageUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
public class BaseProgrammeController {
    @Resource
    ProgrammeService programmeService;

    public DaoPage loadProgrammePage(Map parameter, Integer pageIndex,
                                     Integer pageSize) throws Exception {
        DaoPage daoPage = programmeService.findProgrammeByParams(parameter, pageIndex, pageSize);
        return daoPage;
    }

    public Collection<ProgrammeVO> loadProgrammeList(Map parameter) throws Exception {
        List result = programmeService.findProgrammeByParams(parameter);
        return result;
    }

    public ProgrammeVO loadAddProgramme(Map parameter) throws Exception {
        ProgrammeVO vo = new ProgrammeVO();
        return vo;
    }

    public ProgrammeVO loadModifyProgramme(Map parameter) throws Exception {
        Object id = parameter.get("id");
        return loadModifyProgramme(id);
    }

    public ProgrammeVO loadModifyProgramme(Object id) throws Exception {
        AssertHelper.notEmpty_assert(id, "编辑的主键不能为空");
        ProgrammeEntity entity = (ProgrammeEntity) programmeService.get(ProgrammeEntity.class, id.toString());
        ProgrammeVO vo = programmeService.convertProgrammeToVO(entity);
        return vo;
    }

    public void saveProgramme(Collection<ProgrammeVO> objs) throws Exception {
        Map results = PageUtils.assembleDatasetMap(objs);
        programmeService.saveProgrammeDataListsMap(results);
    }

    public void saveAddProgramme(ProgrammeVO vo) throws Exception {
        ProgrammeEntity entity = programmeService.convertProgrammeVOToEntity(vo);
        if (entity.getId() == null) {
            programmeService.save(entity);
        } else {
            programmeService.update(entity);
        }
        vo.setId(entity.getId());
    }

    public void updateModifyProgramme(ProgrammeVO vo) throws Exception {
        ProgrammeEntity entity = (ProgrammeEntity) programmeService.get(ProgrammeEntity.class, vo.getId());
        ReflectUtils.copyProperties(vo, entity);
        programmeService.update(entity);
    }

    public DaoPage loadProject(Map parameter, Integer pageIndex,
                               Integer pageSize) throws Exception {
        DaoPage daoPage = programmeService.findProjectByParams(parameter, pageIndex, pageSize);
        return daoPage;
    }

    public Collection loadProject(Map parameter) throws Exception {
        List result = programmeService.findProjectByParams(parameter);
        return result;
    }

    public void saveProject(Collection<ProjectVO> objs) throws Exception {
        Map results = PageUtils.assembleDatasetMap(objs);
        programmeService.saveProjectDataListsMap(results);
    }
}

